Skip to content

gh-149221: Handle corner case in random.binomialvariate()#149266

Closed
rhettinger wants to merge 1 commit intopython:mainfrom
rhettinger:binomialvariate_corner_case
Closed

gh-149221: Handle corner case in random.binomialvariate()#149266
rhettinger wants to merge 1 commit intopython:mainfrom
rhettinger:binomialvariate_corner_case

Conversation

@rhettinger
Copy link
Copy Markdown
Contributor

@rhettinger rhettinger commented May 2, 2026

@rhettinger rhettinger added type-bug An unexpected behavior, bug, or error skip news needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels May 2, 2026
Comment thread Lib/random.py
Comment on lines +839 to +843
try:
y += _floor(_log2(random()) / c) + 1
except ValueError:
# Handle rare case of where random() gives 0.0
continue
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is try...except statement necessary here?

I think it's better to write like y += _floor(_log2(1.0 -random()) / c) + 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in #149222 - it's an optimization.

@rhettinger rhettinger closed this May 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants